﻿<?php
include 'PublicdetailController.php';
class KhachhangController extends PublicdetailController {
	private $_MKhachhang;
	private $_passport;
	private $_Khachhang = array ();
	public $idnguoidung;
    public $nguoidung;
    public function init() {
		     $this->initValue ();
			 $this->idnguoidung = $this->TblUserbyId[0]['id'];
             $this->nguoidung = $this->TblUserbyId[0]['name'];
		     $this->_MKhachhang = new Model_Khachhang();
             $this->_passport = @$_REQUEST ['passport'];
			 $timestamp = strtotime(@$_REQUEST ['birthDate']);
             $date = date('Y-m-d',$timestamp );			 
			 $this->_Khachhang = array (
					'passport' => $this->_passport,
					'name' => @$_REQUEST ['name'],
					'gender' =>  @$_REQUEST ['gender'],
					'email'  => @$_REQUEST ['email'],
					'phone'  => @$_REQUEST ['phone'],
					'address'  => @$_REQUEST ['address'],
					'company'  => @$_REQUEST ['company'],
					'companyPhone'  => @$_REQUEST ['companyPhone'],
					'companyAddress'  => @$_REQUEST ['companyAddress'],
					'birthDate'  => $date,
					
			);
		}
    public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != 'login') {
				$this->_redirect ( '/index/login' );
			}
		}
	}
    public function indexAction() {
    if($this->TblUserbyId[0]['roles']!='Admin' && $this->TblUserbyId[0]['roles']!='Quanly')
		{
		echo "<script type=\"text/javascript\">
			    alert('Bạn không có quyền vào mục này!');
				 window.history.back();
			</script>";
		die;
		}


      }      
     public function jsonAction() {
	    $page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$sort = isset ( $_POST ['sort'] ) ? strval ( $_POST ['sort'] ) : 'name';
		$order = isset ( $_POST ['order'] ) ? strval ( $_POST ['order'] ) : 'asc';
		$offset = ($page - 1) * $rows;
		$this->_helper->layout ()->disableLayout ();
		$jsonObj = json_encode($this->_MKhachhang->getItems($sort, $order, $offset, $rows));        
		return $this->view->items = $jsonObj;	
	}
	public function ordersAction() {
	    $this->_helper->layout ()->disableLayout ();
	    $id = $this->_getParam ( 'id' );
		$jsonObj = $this->_MKhachhang->getOrdersById($id);  	
		return $this->view->items = $jsonObj;
	}
	public function listhdAction() {
	    $this->_helper->layout ()->disableLayout ();
		$id = $this->_getParam ( 'id' );
	    //$passport = $this->_getParam ( 'id' );
		$jsonObj = json_encode($this->_MKhachhang->getListHd($id)); 	
		return $this->view->items = $jsonObj;
	}

	
	public function addAction() {
		$this->_helper->layout ()->disableLayout ();
		$jsonObj = array ();
		$dup = $this->_MKhachhang->dupmm ($this->_passport );
		if ($dup > 0) {
			echo "<script type=\"text/javascript\">
			    alert(' Số chứng minh đã có!');
			</script>";
			$jsonObj ["success"] = false;
			return $this->view->items = json_encode ( $jsonObj );
		} else {
			$this->_MKhachhang->addObj ( $this->_Khachhang );
			$jsonObj ["success"] = true;
		}
		return $this->view->items = json_encode ( $jsonObj );
	}

	
	public function updatesAction() {
		$id = $this->_getParam ( 'id' );
		$this->_helper->layout ()->disableLayout ();
		$jsonObj = array ();
	    $this->_MKhachhang->update( $id, $this->_Khachhang );
        $jsonObj ["success"] = true;
		return $this->view->items = json_encode ( $jsonObj );
	}
	
	public function delAction() {
		$this->_helper->layout ()->disableLayout ();
		$id = @$_REQUEST ['id'];
		// $id = $this->_getParam ( 'id' );
		$_Id = null;
		$jsonObjv = null;
		
		if (isset ( $id ) && $id > 0) {
			$_Id = $this->_MKhachhang->del ( $id );
		} else {
			return;
		}
		
		if (! $_Id) {
			$jsonObj = json_encode ( array (
					'success' => false 
			) );
		} else {
			$jsonObj = json_encode ( array (
					'success' => true 
			) );
		}
		return $this->view->items = $jsonObj;
	}
	
	public function searsAction() {
		$this->_helper->layout ()->disableLayout ();
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$sort = isset ( $_POST ['sort'] ) ? strval ( $_POST ['sort'] ) : 'name';
		$order = isset ( $_POST ['order'] ) ? strval ( $_POST ['order'] ) : 'asc';
		$offset = ($page - 1) * $rows;
		$ten = $this->_getParam ( 'name' );
		$socmt = $this->_getParam ( 'passport' );
		$tu = $this->_getParam ( 'sTu' );
		$den = $this->_getParam ( 'sDen' );
		try {
			$jsonObj = json_encode ( $this->_MKhachhang->searObj ( $ten, $socmt, $tu, $den, $sort, $order, $offset, $rows ) );
			return $this->view->jsonObj = $jsonObj;
		} catch ( Exception $e ) {
			echo "<script type=\"text/javascript\">
			alert('Exception!');
			</script>";
		}
	}
	
	
	public function thanhtoannoAction() {      
		$id =  @$_REQUEST['id'];
		$ketqua = array (
					'debit' => 0,
                    'createdBy'	=> $this->nguoidung.'-đã thanh toán nợ',
                    'creeatedById' =>$this->idnguoidung,						
			);
	
		if($id){
			$this->_MKhachhang->thanhtoanno( $id, $ketqua );
		}
		die;
	}
	
	public function cthanhtoanAction() {      
		$id =  @$_REQUEST['id'];
		$ketqua = array (
					'debit' => 1,
                    'createdBy'	=> $this->nguoidung.'-đã ghi nợ',
                    'creeatedById' =>$this->idnguoidung,					
			);
		
		if($id){
			$this->_MKhachhang->thanhtoanno( $id, $ketqua );
		}
		die;
	}
	
	
}